package com.herui.sdyu_lib.blu;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;

import com.herui.sdyu_lib.blu.utils.BAction;
import com.herui.sdyu_lib.blu.utils.BleService;
import com.herui.sdyu_lib.blu.utils.Blis;
import com.herui.sdyu_lib.blu.utils.BluService;
import com.herui.sdyu_lib.utils.LogUtils;
import com.herui.sdyu_lib.utils.ToastUtil;


/**
 * Created by amanda.shan on 2017/8/16.
 */

public abstract class BaseBluService implements DeviceServiceLis, Blis {

    protected BlueDevice mBlueDevice;
    protected int connectState = BluDeviceConfig.NOTCONNECT;
    protected BluetoothAdapter mBluetoothAdapter;

    protected MsgListener msgListener;
    protected BAction bAction;

    private Context context;

    public BaseBluService(Context context) {
        this.context = context;
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }



    /**
     * 设置蓝牙连接状态
     *
     * @param state
     */
    @Override
    public void bluConenctState(int state) {
        setState(state);
    }

    @Override
    public void setListener(MsgListener listener) {
        msgListener = listener;
    }

    @Override
    public void setBlueDevice(BlueDevice blueDevice) {
        if (mBlueDevice != null && !mBlueDevice.getBlueMac().equals(blueDevice.getBlueMac()) &&
                connectState != BluDeviceConfig.NOTCONNECT) {
            disBluConnect();
        }
        mBlueDevice = blueDevice;
    }

    @Override
    public void ask() {

        LogUtils.d("=================ask================");
        msgListener.onDeviceState(mBlueDevice, connectState);
    }

    @Override
    public void connect() {
        if (mBlueDevice == null)
            LogUtils.e("mBlueDevice == null  未设置连接的多惨设备");
        else
            startBluConnect();
    }

    public int getMostWaitTime(){
        return 60*60*1000;
    }

    /**
     * 开始连接
     */
    protected void startBluConnect() {
//        if (connectState != DeviceConfig.NOTCONNECT) {
//            msgListener.sendDeviceState(mBlueDevice, connectState);
//            return;
//        }
        if (mBluetoothAdapter == null) {
            ToastUtil.showMessage("本机不支持蓝牙");
        } else if (mBlueDevice == null) {
            ToastUtil.showMessage("未绑定设备,请先绑定设备");
        } else if (!mBluetoothAdapter.isEnabled()) {
            mBluetoothAdapter.enable();
            ToastUtil.showMessage("蓝牙未打开");
        } else {
            if (bAction != null) {
                bAction.disConnect();
            }
            if (bAction != null) {
                //有种情况是在disConnect后依然报空指针bAction，所以这里单独判断
                bAction.setBlis(null);
            }

            if (mBlueDevice.isBle())
                bAction = new BleService(this, context);
            else
                bAction = new BluService(this, mBluetoothAdapter);
            bAction.connect(mBlueDevice);

            bAction.setMostWaitTime(getMostWaitTime());
        }
    }

    @Override
    public void send(byte[] bytes) {
        send(bytes, bytes.length);
    }

    @Override
    public void send(byte[] bytes, int len) {
        if (bAction != null)
            bAction.send(bytes, len);
    }

    /**
     * 断开连接
     */
    @Override
    public void disBluConnect() {

        if (bAction == null) {
            return;
        }

        LogUtils.i("断开蓝牙连接");
        if (bAction != null)
            bAction.disConnect();
        bAction = null;

        setState(BluDeviceConfig.NOTCONNECT);
    }


    /**
     * 设置整体连接状态
     *
     * @param connectState
     */
    protected void setState(int connectState) {

        if (this.connectState == connectState)
            return;
        LogUtils.d("BaseBluService setState:" + connectState);
        this.connectState = connectState;
        msgListener.onDeviceState(mBlueDevice, connectState);
    }

}
